package com.tl.sun.ui.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.flyco.tablayout.SlidingTabLayout;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.tl.sun.R;
import com.tl.sun.manager.MDModel;
import com.tl.sun.utils.ToastUtil;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 作者：HQY on 17/8/5 17:36
 * 邮箱：hqy_xz@126.com
 */

public class ProvinceLineFragment extends Fragment {

    @Bind(R.id.rl_generic_back)
    RelativeLayout mRlGenericBack;
    @Bind(R.id.tv_generic_title)
    TextView mTvGenericTitle;
    @Bind(R.id.tl_fqa_top)
    SlidingTabLayout mTlFqaTop;
    @Bind(R.id.vp_fqa_bottom)
    ViewPager mVpFqaBottom;
    private ArrayList<Fragment> mFragments = new ArrayList<>();
    private String mTitles[];
    private MyPagerAdapter mAdapter;

    private View rootView;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (rootView == null) {
            rootView = inflater.inflate(R.layout.activity_line_province, container, false);
            ButterKnife.bind(this, rootView);
            init();
        }
        ViewGroup parent = (ViewGroup) rootView.getParent();
        if (parent != null) {
            parent.removeView(rootView);
        }
        mRlGenericBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getActivity().finish();
            }
        });

        ButterKnife.bind(this, rootView);
        return rootView;
    }

    private void init() {
        mTvGenericTitle.setText("线路选择");
        mFragments.add(LineFragment.newInstance("false", "2"));
        mFragments.add(LineFragment.newInstance("false", "1"));
        mFragments.add(LineFragment.newInstance("true", "1"));
        mTitles = getActivity().getResources().getStringArray(R.array.line_select_one);
        mAdapter = new MyPagerAdapter(getActivity().getSupportFragmentManager());
        mVpFqaBottom.setAdapter(mAdapter);
        mTlFqaTop.setViewPager(mVpFqaBottom);
        mVpFqaBottom.setCurrentItem(0);
        mVpFqaBottom.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (position != 0) {
                    if (!MDModel.getInstance().getIsSenior().equals("2")) {
                        mVpFqaBottom.setCurrentItem(0);
                        mTlFqaTop.setCurrentTab(0);
                        ToastUtil.showToast("当前套餐只有高级VIP可以使用");
                    }
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        mTlFqaTop.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                if (position != 0) {
                    if (!MDModel.getInstance().getIsSenior().equals("2")) {
                        mVpFqaBottom.setCurrentItem(0);
                        mTlFqaTop.setCurrentTab(0);
                        ToastUtil.showToast("当前套餐只有高级VIP可以使用");
                    }
                }
            }

            @Override
            public void onTabReselect(int position) {

            }
        });
    }

    private class MyPagerAdapter extends FragmentPagerAdapter {
        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mTitles[position];
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }
    }

}
